/* promise 形式getsetting */
const getSetting=()=>{
  
    return new Promise((resolve, reject) => {
      wx.getSetting({
          success: (result)=>{
            resolve(result)
          },
          fail: (err)=>{
            reject(err)
          },
          complete: ()=>{}
      })
  })
}

/* promise 形式chooseAddress */
const getLocation=()=>{
  
  return new Promise((resolve, reject) => {
    wx.getLocation({
      type: 'gcj02',
      altitude: true,//高精度定位
        success: (result)=>{
          resolve(result)
        },
        fail: (err)=>{
          reject(err)
        },
        complete: ()=>{}
    })
})
}

/* promise 形式chooseAddress */
const chooseAddress=()=>{
  
    return new Promise((resolve, reject) => {
      wx.chooseAddress({
          success: (result)=>{
            resolve(result)
          },
          fail: (err)=>{
            reject(err)
          },
          complete: ()=>{}
      })
  })
}
/* promise chooseImage */
const chooseImage=()=>{
  
  return new Promise((resolve, reject) => {
    wx.chooseImage({
      count: 4, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], //从相册选择
        success: (result)=>{
          resolve(result)
        },
        fail: (err)=>{
          reject(err)
        },
        complete: ()=>{}
    })
}).catch((e) => {})
}
/* promise 形式chooseVideo */
const chooseVideo=()=>{
  
  return new Promise((resolve, reject) => {
    wx.chooseVideo({
       sourceType: ['camera'],
      /* sourceType: ['album', 'camera'],*/
       maxDuration: 60,
       camera: 'back',
        success: (result)=>{
          resolve(result)
        },
        fail: (err)=>{
          reject(err)
        },
        complete: ()=>{}
    })
}).catch((e) => {})
}
/* promise 形式openSetting */
const openSetting=()=>{
  
    return new Promise((resolve, reject) => {
      wx.openSetting({
          success: (result)=>{
            resolve(result)
          },
          fail: (err)=>{
            reject(err)
          },
          complete: ()=>{}
      })
  })
}
/* promise 形式wx.downloadFile */
const downloadFile=(url)=>{
   
  return new Promise((resolve, reject) => {
    wx.downloadFile({
        url: url,      
        success: (result)=>{         
          resolve(result)
        },
        fail: (err)=>{       
          wx.showModal({
            title: '网络错误',
            content: '下载文件出错，请重新尝试',
            showCancel: false
          })
          
          reject(err)
        },
        complete: ()=>{
         
        }
    })
})
}

/* promise 形式wx.saveImageToPhotosAlbum */
const saveImageToPhotosAlbum=(filePath)=>{
   
  return new Promise((resolve, reject) => {
    wx.saveImageToPhotosAlbum({
       filePath: filePath,      
        success: (result)=>{         
          resolve(result)
        },
        fail: (err)=>{       
          wx.showModal({
            title: '错误',
            content: '保存文件失败，请重新尝试',
            showCancel: false
          })
          if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
            console.log("当初用户拒绝，再次发起授权")
          } 
          resolve(err)
        }
    })
})
}

/* promise 形式uploadFile */
const uploadFile=(url,filePath,name,formData)=>{
  wx.showLoading({
    title: '加载中',
  })  
  return new Promise((resolve, reject) => {
    wx.uploadFile({
        url: url,
        filePath:filePath,　//待上传的图片，由 chooseImage获得
        name:name,
        header: { 
          'Content-Type': 'multipart/form-data',
          'Authorization': wx.getStorageSync("token"),  //如果需要token的话要传
        },
        formData: formData, // HTTP 请求中其他额外的 form data
        success: (res)=>{
         // console.log('res=',res)
         //  console.log('res.data.code=',JSON.parse(res.data).code)
          if(JSON.parse(res.data).code!=200){
            wx.showModal({
              title: '错误',
              content: JSON.parse(res.data).message,
              showCancel: false
            })
          } 
          resolve(res)
        },
        fail: (err)=>{
          //console.log('reqest error', err)
       
          wx.showModal({
            title: '网络错误',
            content: '上传出错，请重新上传',
            showCancel: false
          })
          
          reject(err)
        },
        complete: ()=>{
          wx.hideLoading()
        }
    })
})
}

/* promise 形式checkSession */
const checkSession=()=>{
  
  return new Promise((resolve, reject) => {
    wx.checkSession({        
        success: (result)=>{
          resolve(result)
        },
        fail: (err)=>{
          reject(err)
        }
    })
})
}

/* promise 形式login */
const login=()=>{
  
  return new Promise((resolve, reject) => {
    wx.login({
        timeout:10000,
        success: (result)=>{
          resolve(result)
        },
        fail: (err)=>{
          reject(err)
        },
        complete: ()=>{}
    })
})
}

/* promise 形式requestPayment 

   {obj} pay 支付所必要的参数
*/
const requestPayment=(pay)=>{
  
  return new Promise((resolve, reject) => {
    wx.requestPayment({
        ...pay,
        success: (result)=>{
          resolve(result)
        },
        fail: (err)=>{
          reject(err)
        },
        complete: ()=>{}
    })
})
}

/* promise 形式showModal */
const showModal=({content,showCancel})=>{
  //console.log(content)
  return new Promise((resolve, reject) => {
    wx.showModal({
      title: '提示',
      content: content,
      showCancel: showCancel,//true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#3CC51F',
        success: (result)=>{
          resolve(result)
        },
        fail: (err)=>{
          reject(err)
        },
        complete: ()=>{}
    })
  })
}

/* promise 形式showToast */
const showToast=({title,icon})=>{
  //console.log(content)
  return new Promise((resolve, reject) => {
    wx.showToast({
        title: title,
        icon: icon,
        image: '',
        duration: 1500,
        mask: false,
        success: (result)=>{
          resolve(result)
        },
        fail: (err)=>{
          reject(err)
        },
        complete: ()=>{}
    })
  })
}


module.exports = {
    openSetting: openSetting,
    chooseAddress: chooseAddress,
    getSetting: getSetting,
    showModal:showModal,
    showToast:showToast,
    login:login,
    requestPayment:requestPayment,
    chooseVideo:chooseVideo,
    chooseImage:chooseImage,
    uploadFile:uploadFile,
    checkSession:checkSession,
    getLocation:getLocation,
    downloadFile:downloadFile,
    saveImageToPhotosAlbum:saveImageToPhotosAlbum
  }

